package net.homelinux.chaoswg.io.renga.krypto.animations.Split2;

import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

import net.homelinux.chaoswg.io.datatypes.Pair;
import net.homelinux.chaoswg.io.higherorderfunctions.UnaryFunction;
import net.homelinux.chaoswg.io.renga.core.registries.GlobalPropertyRegistry;
import net.homelinux.chaoswg.io.renga.krypto.datatypes.numerical.UnsignedNBit;
import net.homelinux.chaoswg.io.renga.krypto.operations.type.Split2;

public class Split2StateChanger implements Runnable {
    final UnaryFunction<Pair<UnsignedNBit, UnsignedNBit>, UnsignedNBit> split2 = new Split2 ();
    final Split2State initialState;
    JLabel jlIn = null;
    JLabel jlout1 = null;
    JLabel jlout2 = null;
    JFrame jw;

    public Split2StateChanger(Split2State initialState) {
        this.initialState = initialState;

        jw = new JFrame ("Split-2");
        jw.getContentPane().add(getPanel());
        jw.setLocationRelativeTo(null);
        
        jw.setSize(500, 100);
    }

    public JPanel getPanel() {
        final JPanel toReturn = new JPanel (new GridLayout(2,1));
        final JPanel south = new JPanel (new GridLayout(1,2));
        south.add (jlout1 = new JLabel (initialState.getOut().getFirst().toString()));
        south.add (jlout2 = new JLabel (initialState.getOut().getSecond().toString()));


        toReturn.add(jlIn = new JLabel (initialState.getIn().toString()));

        toReturn.add(south);
        toReturn.setBorder(new BevelBorder (BevelBorder.RAISED));

        jlIn.setHorizontalAlignment(JLabel.CENTER);
        jlout1.setHorizontalAlignment(JLabel.CENTER);
        jlout2.setHorizontalAlignment(JLabel.CENTER);

        return toReturn;
    }
    
    private void setNew () {
        jlIn.setText(initialState.getIn().toHexString());
        jlIn.paintImmediately(jlIn.getVisibleRect());
        jlout1.setText(initialState.getOut().getFirst().toHexString());
        jlout1.paintImmediately(jlout1.getVisibleRect());
        jlout2.setText(initialState.getOut().getSecond().toHexString());
        jlout2.paintImmediately(jlout2.getVisibleRect());
    }

    public void run() {
        int delay = ((Integer)GlobalPropertyRegistry.getGlobalPropertyRegistry().getProperty("animationSpeed")) * 100;
        
        jw.setVisible(true);

        UnsignedNBit input = initialState.getIn();
        Pair<UnsignedNBit, UnsignedNBit> out = split2.apply(input);
        setNew();
        try {
            Thread.sleep(delay);
        } catch (InterruptedException ie) {}

        initialState.setOut(out);
        setNew();
        try {
            Thread.sleep(delay);
        } catch (InterruptedException ie) {}
        
//        initialState.getOut().setFirst(out.getFirst());
//        setNew ();
//        try {
//            Thread.sleep(delay);
//        } catch (InterruptedException ie) {}
//
//        initialState.getOut().setSecond(out.getSecond());
//        setNew ();
//        try {
//            Thread.sleep(delay);
//        } catch (InterruptedException ie) {}
//
    }
    
    public void close () {
        jw.dispose();
    }

}
